package com.advaita.licenseManager;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;

import com.advaita.license.Customer;
import com.advaita.license.LicenseKey;
import com.advaita.license.Selection;
import com.advaita.license.License;

import java.awt.Color;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JList;
import javax.swing.BoxLayout;

import net.miginfocom.swing.MigLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class CustomerInfo extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldCustomerName;
	private JTextField textFieldExpirationDate;
	private Customer customer;
	private JTextField textFieldLicense;
	JList<Selection> listLeft;
	private DefaultListModel<Selection> listModelLeft = new DefaultListModel<Selection>();
	JList<Selection> listRight;
	private DefaultListModel<Selection> listModelRight = new DefaultListModel<Selection>();
	private JTextField textFieldMacAddress;
	ArrayList<Selection> list;

	/**
	 * Create the panel.
	 */
	public CustomerInfo(Customer cust) {
		
		customer = cust;
		setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("17px"),
				ColumnSpec.decode("80px:grow"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),
				ColumnSpec.decode("17px"),},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"),
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));

		JLabel lblTitle = new JLabel("Customer Information");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 16));
		add(lblTitle, "2, 2, 9, 1, center, top");
		
		JLabel lblCustomerName = new JLabel("Customer Name:");
		add(lblCustomerName, "4, 6, right, center");
		
		textFieldCustomerName = new JTextField();
		add(textFieldCustomerName, "6, 6, 3, 1, fill, default");
		textFieldCustomerName.setColumns(10);
		
		JLabel lblExpirationDate = new JLabel("Expiration Date:");
		add(lblExpirationDate, "4, 8, right, center");
		
		textFieldExpirationDate = new JTextField();
		add(textFieldExpirationDate, "6, 8, 3, 1, fill, default");
		textFieldExpirationDate.setColumns(10);
		
		JLabel labelMacAddress = new JLabel("Mac Address:");
		add(labelMacAddress, "4, 10, right, default");
		
		textFieldMacAddress = new JTextField();
		add(textFieldMacAddress, "6, 10, 3, 1, fill, default");
		textFieldMacAddress.setColumns(10);
		
		JPanel panelBottom = new JPanel();
		panelBottom.setBackground(Color.CYAN);
		add(panelBottom, "2, 12, 9, 1, fill, fill");
		panelBottom.setLayout(new BorderLayout(0, 0));
		
		JPanel panelButtons = new JPanel();
		panelBottom.add(panelButtons, BorderLayout.NORTH);
		panelButtons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnOrganism = new JButton("Organisms");
		btnOrganism.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LoadOrganisms();
			}
		});
		panelButtons.add(btnOrganism);
		
		JButton btnMicroArray = new JButton("Micro Arrays");
		btnMicroArray.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadMicroArray();
			}
		});
		panelButtons.add(btnMicroArray);
		
		JButton btnGeneID = new JButton("Gene ID");
		btnGeneID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadGeneID();
			}
		});
		panelButtons.add(btnGeneID);
		
		JButton btnPathwayDB = new JButton("Pathway DB");
		btnPathwayDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadPathwayDB();
			}
		});
		panelButtons.add(btnPathwayDB);
		
		JButton btnAnalysis = new JButton("Analysis");
		btnAnalysis.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadAnaysis();
			}
		});
		panelButtons.add(btnAnalysis);
		
		JPanel panelLists = new JPanel();
		panelBottom.add(panelLists, BorderLayout.CENTER);
		panelLists.setLayout(new GridLayout(1, 0, 0, 0));
		
		
		JScrollPane scrollPane = new JScrollPane();
		panelLists.add(scrollPane);
		
		listLeft = new JList<Selection>(listModelLeft);
		scrollPane.setViewportView(listLeft);

		JPanel panelMoveButtons = new JPanel();
		panelLists.add(panelMoveButtons);
		panelMoveButtons.setLayout(new BoxLayout(panelMoveButtons, BoxLayout.X_AXIS));
		
		JPanel panelCenterButtons = new JPanel();
		panelMoveButtons.add(panelCenterButtons);
		panelCenterButtons.setLayout(new MigLayout("", "[]", "[][][][]"));
		
		JButton btnMoveRight = new JButton(">>");
		btnMoveRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MoveSelectedItemToRight();
			}
		});
		panelCenterButtons.add(btnMoveRight, "cell 0 1,push ,alignx center");
		
		JButton btnMoveLeft = new JButton("<<");
		btnMoveLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MoveSelectedItemToLeft();
			}
		});
		panelCenterButtons.add(btnMoveLeft, "cell 0 2,push ,alignx center");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panelLists.add(scrollPane_1);
		
		listRight = new JList<Selection>(listModelRight);
		scrollPane_1.setViewportView(listRight);
		
		JPanel panelLicense = new JPanel();
		panelBottom.add(panelLicense, BorderLayout.SOUTH);
		
		JButton btnGenerateKey = new JButton("Generate Key");
		btnGenerateKey.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateCustomer();
				textFieldLicense.setText(customer.licenseInfo.getKey());
			}
		});
		panelLicense.add(btnGenerateKey);
		
		JLabel lblLicenseKey = new JLabel("License Key:");
		panelLicense.add(lblLicenseKey);
		
		textFieldLicense = new JTextField();
		panelLicense.add(textFieldLicense);
		textFieldLicense.setColumns(30);

		PopulateForm();
	}

	private void PopulateForm() {
		textFieldCustomerName.setText(customer.getUser());
		String date = new SimpleDateFormat("MM-dd-yyyy").format(customer.getLicenseInfo().getExpDate());
		textFieldExpirationDate.setText(date);
		if (customer.licenseInfo.getKey() != "No Key Set Yet"){
			Customer tempCustomer = License.decodeSuperSecretCode(customer.getLicenseInfo().getKey());
			customer.setLicenseInfo(tempCustomer.getLicenseInfo());
		}
		textFieldLicense.setText(customer.licenseInfo.getKey());
		textFieldMacAddress.setText(customer.licenseInfo.getMacAddress());
		LoadOrganisms();
	}
	
	public Customer UpdateCustomer() {
		customer.setUser(textFieldCustomerName.getText());
		LicenseKey key = customer.getLicenseInfo();
		Date date = ConvertStringToDate(textFieldExpirationDate.getText());
		key.setExpDate(date);
		key.setKey(textFieldLicense.getText());
		key.setMacAddress(textFieldMacAddress.getText());
		License.generateSuperSecretcode(customer);
		customer.setLicenseInfo(key);
		return customer;
	}
	
	private Date ConvertStringToDate(String date) {
		Date thedate = null;
		try {
			date = textFieldExpirationDate.getText();
			DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
			thedate = df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			thedate = null;
		}
		return thedate;
	}
	
	private void LoadOrganisms(){
		list = customer.licenseInfo.getOrganisms();
		LoadList();
	}
	
	private void LoadMicroArray(){
		list = customer.licenseInfo.getMicroArray();
		LoadList();
	}

	private void LoadGeneID(){
		list = customer.licenseInfo.getGeneID();
		LoadList();
	}

	private void LoadPathwayDB(){
		list = customer.licenseInfo.getDatabase();
		LoadList();
	}

	private void LoadAnaysis(){
		list = customer.licenseInfo.getAnalysis();
		LoadList();
	}

	private void LoadList(){
		listModelLeft.clear();
		listModelRight.clear();
		
		for(Selection sel: list) {
			if(sel.getIsEnabled()){
				listModelRight.addElement(sel);
			} else {
				listModelLeft.addElement(sel);
			}
		}
	}
	
	private void MoveSelectedItemToRight() {
		List<Selection> selections = listLeft.getSelectedValuesList();

		for (int i = 0; i < selections.size(); i++) {
			Selection selectedItem = selections.get(i);
			selectedItem.setIsEnabled(true);
			LoadList();
		}
	}
	
	private void MoveSelectedItemToLeft() {
		List<Selection> selections = listRight.getSelectedValuesList();

		for (int i = 0; i < selections.size(); i++) {
			Selection selectedItem = selections.get(i);
			selectedItem.setIsEnabled(false);
			LoadList();
		}
	}

	private void GenerateKey() {
		
	}
}
